/**
 * Created by Administrator on 2017/3/8.
 * mysql连接池类
 */

var mysql = require("mysql");

var db_config = {
    host: '127.0.0.1',
    user: 'root',
    password: '123456',
    database: 'love_db',
    charset:'UTF8_GENERAL_CI'
};

/**
 * 数据库连接池
 * @constructor
 */
function OptPool(){
    var flag = false; //是否连接过
    this.pool = mysql.createPool(db_config); //初始化连接池

    this.getPool = function(){
        if(!this.flag){  //从未获获取过连接
            //监听connectioin事件
            this.pool.on("connection",function(connection) {
                connection.query("SET SESSION auto_increment_increment");
                console.log("the pool init successful..");
                this.flag = true;
            });
            this.pool.on("error" , function (error) {
                console.log("connection error : "+error);
            });
        }
        return this.pool;
    }
}

module.exports = OptPool;